﻿$(function () {    

    UIHandler();

    $('#ref-login').bind('click', function () {        
        Login();
        return false;
    });

    $('#ref-register').bind('click', function () {
        Register();
        return false;
    });

    $('#ref-ex-register').bind('click', function () {
        Register();
    });

    HandleParameter();

    function Register() {
        var form = $('#register-form');
        var className = 'hide';

        $('.popup').slideUp('fast').addClass(className);

        if (form.hasClass(className)) {
            form.slideDown('fast', function () {
                $(this).removeClass(className)
            });
        }

        form.find('.close').bind('click', function () {
            form.slideUp('fast', function () {
                $(this).addClass(className);
            });
        });

        form.find('#register-submit').bind('click', function () {
            var email = form.find('#register-email').val().trim();
            var password = form.find('#register-password').val();
            var remember = form.find('#register-remember').is(':checked') ? true : false;
            
            if (CheckAuthenBasic(form, email, password) == false) return;

            var data = {
                "email": email,
                "password": password,
                "returnUrl": window.location.href + '?t=login',
                "tpl": GetParameterByName('tpl')
            };
            $.post('/Account/Register', data, function (response) {
                var status = response.status;
                if (status == 0) {
                    // email or password is wrong
                }
                else if (status == 1) {
                    // redirect to target url
                    window.location.href = response.returnUrl;
                }
            });
        });
    }

    function Login() {
        
        var form = $('#login-form');
        var className = 'hide';

        $('.popup').slideUp('fast').addClass(className);

        if (form.hasClass(className)) {
            form.slideDown('fast', function () {
                $(this).removeClass(className)
            });
        }

        form.find('.close').bind('click', function () {
            form.slideUp('fast', function () {
                $(this).addClass(className);
            });
        });

        form.find('#login-submit').bind('click', function () {
            var email = form.find('#login-email').val();
            var password = form.find('#login-password').val();
            var remember = form.find('#login-remember').is(':checked') ? true : false;

            if (CheckAuthenBasic(form, email, password) == false) return;

            var url = window.location.href;
            
            var data = {
                "email": email,
                "password": password,
                "remember": remember,
                "returnUrl": url
            };

            $.post('/Account/Login', data, function (response) {
                var status = response.status;
                if (status == 0) {
                    // email or password is wrong
                }
                else if (status == 1) {
                    // redirect to target url
                    window.location.href = response.returnUrl;
                }
            });
        });
    }

    function CheckAuthenBasic(form, email, password) {
        var error = form.find('.error');

        error.text('');

        if (email == '') {
            error.text('Email không được để trống');
            return false;
        }
        var email_check = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i;
        if (email_check.test(email) == false) {
            error.text('Email không hợp lệ');
            return false;
        }

        if (password == '') {
            error.text('Mật khẩu không được để trống');
            return false;
        }

        return true;
    }

    function GetParameterByName(name) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
        return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    }

    function HandleParameter() {
        var type = GetParameterByName('t');
        switch (type) {
            case 'login':
                $('#ref-login').click();
                break;
            case 'register':
                $('#ref-register').click();
                break;
        }
        
    }

    function UIHandler() {
        var location = $('#location').text();

        if(location.indexOf('Product/Create') > - 1) {
            $('#menu').hide();
            return;
        }

    }
});